import 'reflect-metadata';
import { container } from 'tsyringe';
import { LoggerToken } from '../domain/interfaces/ILogger';
import type { ILogger } from '../domain/interfaces/ILogger';
import { Logger } from '../core/logger';
import { ChatServiceToken } from '../domain/interfaces/IChatService';
import type { IChatService } from '../domain/interfaces/IChatService';
import { ChatService } from '../application/services/chatService';
import { WebSocketManagerToken } from '../domain/interfaces/IWebSocketManager';
import type { IWebSocketManager } from '../domain/interfaces/IWebSocketManager';
import { WebSocketManager } from '../application/services/websocketManager';
import { RoomRepositoryToken } from '../domain/interfaces/IRoomRepository';
import type { IRoomRepository } from '../domain/interfaces/IRoomRepository';
import { RoomRepository } from '../infrastructure/database/roomRepository';
import { MessageRepositoryToken } from '../domain/interfaces/IMessageRepository';
import type { IMessageRepository } from '../domain/interfaces/IMessageRepository';
import { MessageRepository } from '../infrastructure/database/messageRepository';
import { RedisAdapter } from '../infrastructure/redis/redisAdapter';
import { UserRepository } from '../infrastructure/database/userRepository';
import { UserService } from '../application/services/userService';
import { UserServiceToken } from '../domain/interfaces/IUserService';

// 配置依赖注入容器
export class ContainerConfig {
  static configure(): void {
    // 注册单例服务
    container.registerSingleton<ILogger>(LoggerToken, Logger);
    container.registerSingleton<IChatService>(ChatServiceToken, ChatService);
    container.registerSingleton<IWebSocketManager>(WebSocketManagerToken, WebSocketManager);
    container.registerSingleton<IRoomRepository>(RoomRepositoryToken, RoomRepository);
    container.registerSingleton<IMessageRepository>(MessageRepositoryToken, MessageRepository);
    container.registerSingleton(UserServiceToken, UserService);
    container.registerSingleton(UserRepository);

    // 注册需要特殊配置的服务
    container.register('RedisClient', {
      useFactory: () => RedisAdapter.createClient(process.env.REDIS_URL || '')
    });
  }
}

// 类型安全的解析助手
export const di = {
  resolve: <T>(token: any): T => container.resolve<T>(token),
};

export { container };
